//
//  ViewController.m
//  Code_AutoLayOut_Cconstraint
//
//  Created by osoons on 15/7/27.
//  Copyright (c) 2015年 Code_AutoLayOut_Cconstraint. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

{
    int (^oneBlock) (int);

}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];



    oneBlock = ^(int kkk)
    {

        NSLog(@"%@",[NSNumber numberWithInt:kkk]);
        return 50;

    };
    NSLog(@"%@",oneBlock);



    [self viewCenter];
    [self.view updateConstraintsIfNeeded];
    // Do any additional setup after loading the view, typically from a nib.
}




-(void)viewCenter
{

    //蓝色

    UIView *ViewOne = [[UIView  alloc] init];

    ViewOne.backgroundColor =[UIColor blueColor];
    ViewOne.translatesAutoresizingMaskIntoConstraints = NO;

    //红色
    UIView *ViewTwo = [[UIView  alloc] init];

    ViewTwo.backgroundColor =[UIColor redColor];
    ViewTwo.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:ViewOne];

    [self.view addSubview:ViewTwo];



    //添加约束 高度  给viewOne 添加高度约束

    NSLayoutConstraint * hightValues = [NSLayoutConstraint constraintWithItem:ViewOne attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.00 constant:50];
    [ViewOne addConstraint:hightValues];




    //    添加ViewOne左边间距的约束距 父控件20
    CGFloat leftMargin = 20.00;

    NSLayoutConstraint * leftLayOutValues = [NSLayoutConstraint constraintWithItem:ViewOne attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.00 constant:leftMargin];

    [self.view addConstraint:leftLayOutValues];
    //    添加 距父视图度顶部的间距的约束  距父视图

    CGFloat topMargin = 100.00;

    NSLayoutConstraint * topLayOutValues = [NSLayoutConstraint constraintWithItem:ViewOne attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.00 constant:topMargin];

    [self.view addConstraint:topLayOutValues];

    //    添加右间距的约束  距父视图


    CGFloat rightMargin = -20.00;

    NSLayoutConstraint * rightLayOutValues = [NSLayoutConstraint constraintWithItem:ViewOne attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.00 constant:rightMargin];

    [self.view addConstraint:rightLayOutValues];

    /////////////////////////////////////////////////////////////////////

    // viewTwo距ViewOne的右边相等 的约束
    NSLayoutConstraint * rightEqule = [NSLayoutConstraint constraintWithItem:ViewTwo attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:ViewOne attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
    [self.view addConstraint:rightEqule];


    //    viewTwo距ViewOne的顶部的约束 80 添加他们的父控件上

    CGFloat topRightMargin = 20;

    NSLayoutConstraint * topConstraint  = [NSLayoutConstraint constraintWithItem:ViewTwo attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:ViewOne attribute:NSLayoutAttributeBottom multiplier:1.0 constant:topRightMargin];

    [self.view addConstraint:topConstraint];

    // 添加约束 viewTwo的宽度 约束

    CGFloat widthMargin = 20.00;

    NSLayoutConstraint * widthConstraint  = [NSLayoutConstraint constraintWithItem:ViewTwo attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:widthMargin];

    [self.view addConstraint:widthConstraint];//父视图中心的一半

    //  添加view Two的 高度 约束


    CGFloat hightMargin = 100.00;

    NSLayoutConstraint * hightConstraint  = [NSLayoutConstraint constraintWithItem:ViewTwo attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:0.5 constant:hightMargin];

    [ViewTwo addConstraint:hightConstraint];




    //  添加view Two的 高度 约束


    CGFloat hightMargin1 = 200.00;

    NSLayoutConstraint * hightConstraint1  = [NSLayoutConstraint constraintWithItem:ViewTwo attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:hightMargin1];
    
    [ViewTwo addConstraint:hightConstraint1];
    
    


}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
